/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package core.threads.server.creation;
import core.messages.client.creation.Msg_Connect;
import core.threads.server.Server_Public_Data;
import core.threads.server.Server_Public_MsgBox;
import core.threads.server.Server_Public_Thread;
import myTest.MYTest;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import models.Player;
import models.PlayerConnection;

/**
 *
 * @author Invince
 */
public class T_Server_New_Game extends Thread{
/*----------Attributs-------------------*/
    protected boolean stop;
    protected boolean suspend;
/*----------Public functions------------*/
    @Override
    public void run() {
        /**-----Init-----**/
        MYTest.println("Thread:T_Server_New_Game start");
        /**-----Loop----**/
        while(!this.stop){
            if(!this.suspend){
                if(!Server_Public_MsgBox.msgBox_Server_Add_Game.isEmpty()){
                    MYTest.println("Thread:T_Server_New_Game get msg");
                    Msg_Connect msgTemp=Server_Public_MsgBox.msgBox_Server_Add_Game.poll();//从msgBox里取
                    synchronized(Server_Public_Data.semaTempListPlayer){
                       if(Server_Public_Data.tempListPlayer.size()<=10){
                           try {
                                PlayerConnection pC = new PlayerConnection(msgTemp.getClientAdd(), msgTemp.getClientPort());
                                Player p;
                                synchronized(Server_Public_Data.semaIdNextAddPlayer){
                                    p = new Player(Server_Public_Data.idNextAddPlayer++,msgTemp.getSender(), pC);
                                }
                                MYTest.println("Thread:T_Server_New_Game Try to add a player into the TempListPlayer");
                                Server_Public_Data.tempListPlayer.addPlayer(p);//相同名字的player无法加入，cf.Player 下的equals
                            } catch (UnknownHostException ex) {
                                Logger.getLogger(T_Server_New_Game.class.getName()).log(Level.SEVERE, null, ex);
                            } catch (IOException ex) {
                                Logger.getLogger(T_Server_New_Game.class.getName()).log(Level.SEVERE, null, ex);
                            }
                       }else{MYTest.println("Thread:T_Server_New_Game add player failed, cause nbPlayer is already 10");}
                    }
                   Server_Public_MsgBox.refreshScreen();//刷新屏幕
                   if(!Server_Public_MsgBox.msgBox_Server_Next_Step.poll().equals(null)){
                        Server_Public_Thread.tServerCaraNbConfig.resumeT();//唤醒下一步
                        this.suspendT();
                   }
                }
            }
        }
        /**----End-----**/
    }

    public void stopT(){
        synchronized(this){
            this.stop=true;
        }
    }

    public void suspendT(){
        synchronized(this){
            this.suspend=true;
        }
    }

    public void resumeT(){
        synchronized(this){
            this.suspend=false;
        }
    }
/*----------Private functions-----------*/
/*----------Constructor-----------------*/
    public T_Server_New_Game() {
        synchronized(this){
            this.stop=false;
            this.suspend=true;//一开始是悬停的，知道按钮New_Game按下
        }
    }
/*----------Gets and sets---------------*/
}
